package it.univaq.di.helpdesk.business.common.impl.ibatis;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;

import it.univaq.di.helpdesk.business.common.OfficeService;
import it.univaq.di.helpdesk.business.errors.BusinessException;
import it.univaq.di.helpdesk.business.errors.IntegrityConstraintException;
import it.univaq.di.helpdesk.business.model.Office;


public class IBATISOfficeService implements OfficeService {

	private SqlMapClient sqlMap;
	
	public IBATISOfficeService(SqlMapClient sqlMap) {
		super();
		this.sqlMap = sqlMap;
	}

	@Override
	public void addOffice(Office office) throws BusinessException {
		try {
			sqlMap.insert("addOffice",office);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
	}

	@Override
	public void updateOffice(Office office) throws BusinessException {
		try {
			sqlMap.update("editOffice", office);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
	}
	
	@Override
	public List<Office> findAllOffice() throws BusinessException {
		List<Office> offices = null;

		try {
			offices = (List<Office>) sqlMap.queryForList("getOffice");
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return offices;
	}

	@Override
	public Office findOfficeByID(int ID) throws BusinessException {
		Office office = null;

		try {
			office = (Office) sqlMap.queryForObject("Office.getOfficeByID", ID);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return office;
	}

	@Override
	public List<Integer> findBuildingFloor(String building) throws BusinessException {
		List<Integer> floors = null;
		
		try {
			floors = (List<Integer>)sqlMap.queryForList("findBuildingFloor",building);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		return floors;
	}

	@Override
	public List<String> findBuildings() throws BusinessException {
		List<String> buildings = null;
		
		try {
			buildings = (List<String>)sqlMap.queryForList("getBuildings");
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(), e);
		}
		return buildings;
	}

	@Override
	public List<Office> searchForOffices(Map<String, String> queryParam)
			throws BusinessException {
		List<Office> offices = null;
		
		try {
			offices = (List<Office>) sqlMap.queryForList("searchForOffices", queryParam);
		} catch (SQLException e) {
			throw new BusinessException(e.getMessage(),	e);
		}
		
		return offices;
	}

	@Override
	public void deleteOffice(int ID) throws BusinessException {
		try {
			sqlMap.delete("deleteOffice", ID);
		} catch (SQLException e) {
			if (e.getErrorCode() == 2292) {
				throw new IntegrityConstraintException(e.getMessage(), e);
			} else {
				throw new BusinessException(e.getMessage(),	e);
			}
		}
		
	}
}
